home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 026-050 / scopedisk33 / dutils / cmp.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  1KB  |  70 lines

  1. /*
  2.  * CMP.C
  3.  *
  4.  *
  5.  * (C)Copyright 1986, Matthew Dillon, All Rights Reserved.
  6.  * Permission is granted to distribute for non-profit only.
  7.  *
  8.  *    comp file1 file2
  9.  *
  10.  *    Compare two files.  The program will tell you if two files compare
  11.  *    the same or, if not, where the error occured.
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17.  
  18. extern char *malloc();
  19.  
  20. #define BUFSIZE   16384
  21.  
  22. main(ac, av)
  23. char *av[];
  24. {
  25.     long f1, f2;
  26.     register short i, j, n;
  27.     char fail;
  28.     char *buf1, *buf2;
  29.     char *premature_eof = "premature EOF in %s (files compare to that point)\n";
  30.  
  31.     buf1 = malloc(BUFSIZE);
  32.     buf2 = malloc(BUFSIZE);
  33.     if (!buf1 || !buf2) {
  34.     puts("no memory");
  35.     exit(30);
  36.     }
  37.     fail = 0;
  38.     if (ac <= 2) {
  39.     puts ("V2.00 (c)Copyright 1986-1988 Matthew Dillon, All Rights Reserved");
  40.     puts ("cmp file1 file2");
  41.     exit(0);
  42.     }
  43.     f1 = open(av[1], O_RDONLY);
  44.     f2 = open(av[2], O_RDONLY);
  45.     if (f1 && f2) {
  46.     while (!fail && (i = read(f1, buf1, 256))) {
  47.         n = read(f2, buf2, i);
  48.         if (!bcmp(buf1, buf2, n))
  49.         fail = 5;
  50.         if (!fail) {
  51.         if (n == i)
  52.             continue;
  53.         fail = 5;
  54.         }
  55.     }
  56.     if (!fail && read(f2, buf2, 1))
  57.         fail = 5;
  58.     } else {
  59.     puts("Could not open both files");
  60.     fail = 20;
  61.     }
  62.     if (f1 >= 0)
  63.     close(f1);       /* f1 & f2 are either valid or NULL */
  64.     if (f2 >= 0
  65.     close(f2);
  66.     if (fail)
  67.     puts("Compare failed");
  68.     exit(fail);
  69. }
  70.